import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 设置Chromedriver的路径
service = Service(executable_path=r"C:\Users\yckon\Desktop\chromedriver-win64\chromedriver.exe")

# 创建WebDriver对象
driver = webdriver.Chrome(service=service)

# 要访问的PDF文件标识列表
qydm_ids = ['cxPDF/%E5%9B%BD%E5%86%85%E5%85%BD%E8%8D%AF%E8%AF%B4%E6%98%8E%E4%B9%A620211117104748/266%E6%81%A9%E8%AF%BA%E6%B2%99%E6%98%9F%E6%BA%B6%E6%B6%B2%E8%AF%B4%E6%98%8E%E4%B9%A6.pdf']

# 遍历每个PDF文件标识
for qydm in qydm_ids:
    # 构建完整的URL
    url = f'http://vdts.ivdc.org.cn:8081/{qydm}'

    # 访问URL
    driver.get(url)

    # 等待页面加载
    time.sleep(2)

    # 设置页面缩放比例为67%
    driver.execute_script("document.body.style.zoom = 0.67;")

    # 滚动到页面底部以加载整个PDF
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

    # 等待滚动完成并加载页面
    time.sleep(2)

    # 定义截图的坐标和大小
    x = 1430
    y = 1015
    width = 1000  # 根据需要调整宽度
    height = 1000  # 根据需要调整高度

    # 执行局部截图操作
    screenshot = driver.get_screenshot_as_png()
    from PIL import Image

    img = Image.open(BytesIO(screenshot))
    # 裁剪图片，只保留指定坐标和大小的部分
    cropped_img = img.crop((x, y, x + width, y + height))
    # 保存裁剪后的图片
    cropped_img.save(f'screenshot_{qydm.replace("/", "_")}.png')

    # 打印保存的截图文件名
    print(f'Screenshot saved as: screenshot_{qydm.replace("/", "_")}.png')

# 关闭WebDriver
driver.quit()